


// ----------------------------------------------折扣券 、满减券规则--------------------------------------
// 验证优惠共享
export function rule_share(clickTicket, checkedList, roomuselist, vipPriceType) {

  // isDiscountSharing == "1" 优惠折扣不可共享
  let flag = true
  let errMsg = ''
  // 判断当前是否是会员价
  if (clickTicket.IsDiscountSharing == "1" && vipPriceType === true) {
      flag = false
      errMsg = '该券不可与会员权益价共享'
  }
  // 判断当前已选择的券列表(checkedList)里是否有其他类型的券
  if (checkedList.length > 0) {
      checkedList.forEach(item => {
          if ((item.IsDiscountSharing != clickTicket.IsDiscountSharing) || (item.IsDiscountSharing == '1' && item.TicketCode != clickTicket.TicketCode)) {
              flag = false
              errMsg = '该券不可与其他已选择的券优惠共享'
          }
      });
  }
  // 判断当前已使用的券列表(roomuselist)里是否有其他类型的券
  if (roomuselist.length > 0) {
      roomuselist.forEach(item => {
          if ((item.IsDiscountSharing != clickTicket.IsDiscountSharing) || (item.IsDiscountSharing == '1' && item.TicketCode != clickTicket.TicketCode)) {
              flag = false
              errMsg = '该券不可与其他已使用的券优惠共享'
          }
      })
  }
  
    // 暂时新增每种优惠券只能单独使用
    if (checkedList.length > 0) {
        checkedList.forEach(item => {
            if ((item.TypeCode != clickTicket.TypeCode) ) {
                flag = false
                errMsg = '不同种类的优惠券不能混用'
            }
        });
    }
  return { flag, errMsg }
}

// 验证 金额限制条件 不限制 满 每满
export function rule_amt(clickTicket, checkedList, roomuselist, ticketUseList,) {

  let flag = true
  let errMsg = ''
  if (clickTicket.PriceUseCondition == '2') {
      let num = 0
      checkedList.forEach(item => {
          if (item.TicketCode == clickTicket.TicketCode) {
              num += 1
          }
      })
      ticketUseList.forEach(item => {
          if (item.TicketCode == clickTicket.TicketCode) {
              // useCondition:"使用限制：0.无限制，1.桌台使用限制，2.会员使用限制"
              if (clickTicket.UseCondition == '1') {
                  num += item.roomuse
              }
              else if (clickTicket.UseCondition == '2') {
                  num += item.menberuse
              }
          }
      })
 
      if (num >= clickTicket.useCount) {
          flag = false
          errMsg = '已选商品只能用' + clickTicket.useCount + '张此券'
      }
  }
  return { flag, errMsg }
}

// 验证 桌台或者会员 使用张数
export function rule_count(clickTicket, checkedList, ticketUseList,) {

  let flag = true
  let errMsg = ''
  // useCondition:"使用限制：0.无限制，1.桌台使用限制，2.会员使用限制"
  // roomUseNum 桌台限制张数
  // memberUseNum 会员限制张数
  let num = 0
  checkedList.forEach(item => {
      if (item.TicketCode == clickTicket.TicketCode) {
          num += 1
      }
  })
  ticketUseList.forEach(item => {
      if (item.TicketCode == clickTicket.TicketCode) {
          // useCondition:"使用限制：0.无限制，1.桌台使用限制，2.会员使用限制"
          if (clickTicket.UseCondition == '1') {
              num += item.roomuse
          }
          else if (clickTicket.UseCondition == '2') {
              num += item.menberuse
          }
      }
  })

  if (clickTicket.UseCondition == '1') {
      if (num >= clickTicket.RoomUseNum) {
          flag = false
          errMsg = '每张桌台只能使用' + clickTicket.RoomUseNum + '张此券'
      }
  }
  else if (clickTicket.UseCondition == '2') {
      if (num >= clickTicket.MemberUseNum) {
          flag = false
          errMsg = '每个会员只能使用' + clickTicket.MemberUseNum + '张此券'
      }
  }
  return { flag, errMsg }
}


// ---------------------------------------优惠券（旧）-------------------------------------------
// 取消打勾时 需要把已使用张数减-1  // 点击的优惠券 // TicketUseList // 商品列表 // 选中的券的数组
export function returnCheckedTicket(ticket, usedTickets, waresList, checkedList) {
  const { ticketCode, useCondition, } = ticket
  // useCondition:"使用限制：0.无限制，1.桌台使用限制，2.会员使用限制"
  // 1.桌台使用限制 
  if (useCondition == 1) {
      usedTickets.forEach(item => {
  
          if (item.ticketCode == ticketCode) {
              item.roomuse -= 1
          }
      });
  }
  // 2.会员使用限制
  else if (useCondition == 2) {
      usedTickets.forEach(item => {
          if (item.ticketCode == ticketCode) {
              item.menberuse -= 1
          }
      });
  }

  checkedList.forEach((item, index) => {
      if (item.ticketCode == ticketCode) {
          checkedList.splice(index, 1)
      }
  })

}

// 验证桌台使用限制张数
export function table_use(ticket, usedTickets, waresList) {


  let flag = true
  let errMsg = ''
  // useCondition:"使用限制：0.无限制，1.桌台使用限制，2.会员使用限制"
  const { ticketCode, useCondition, roomUseNum } = ticket
  if (useCondition != 1) return { flag, errMsg }
  usedTickets.forEach(item => {
      if (item.ticketCode == ticketCode) {
          // 可使用时张数+1 roomUseNum
          if (item.roomuse < roomUseNum) {
              item.roomuse += 1
              flag = true
          } else {
              flag = false
              errMsg = '超过可使用张数'
          }
      }
  });
  return { flag, errMsg }
}

// 验证会员使用限制张数
export function menber_use(ticket, usedTickets, waresList) {

  let flag = true
  let errMsg = ''
  // useCondition:"使用限制：0.无限制，1.桌台使用限制，2.会员使用限制"
  const { ticketCode, useCondition, memberUseNum } = ticket
  if (useCondition != 2) return { flag, errMsg }
  usedTickets.forEach(item => {
      if (item.ticketCode == ticketCode) {
          // 可使用时张数+1
          if (item.menberuse < memberUseNum) {
              item.menberuse += 1
              flag = true
          } else {
              flag = false
              errMsg = '超过可使用张数'
          }
      }
  });
  return { flag, errMsg }
}

// 验证n个商品用的券不能超过n张
export function count_use(ticket, checkedList, waresList) {
  let flag = true
  let errMsg = ''
  // goodsCode   uomCode
  let waresCount = 0
  waresList.forEach(wares => {
      if (wares.goodsCode == ticket.GoodsCode && wares.uomCode == ticket.UomCode) {
          waresCount += 1
      }
  });

  let checkedCount = 0
  checkedList.forEach(checked => {
      if (checked.goodsCode == ticket.goodsCode && checked.uomCode == ticket.uomCode) {
          checkedCount += 1
      }
  });

  if (checkedCount < waresCount) return { flag, errMsg }
  flag = false
  errMsg = '选择数量大于商品数量'
  return { flag, errMsg }
}